<?php
/**
 * Users controller class
 *
 */

class ScriptsController extends Admin_Controller
{

	public function init()
    {
    	parent::init();
    	//$this->_table = &$this->table_groups;
    	//$this->setActiveCatSite(8);
    	//$this->setActiveCat(96);
    }

    /**
     * index action method
     *
     */
    public function indexAction()
    {
    	die();
    }

    public function tagsartcountAction()
    {
    	$data = Zend_Registry::get('db')->select()
					->from('tags_to_articles', array("id_tag", "count" =>"count(id_tag)"))
					->group('id_tag')
					->query()
					->fetchAll(Zend_Db::FETCH_ASSOC);

		Zend_Registry::get('db')->update('tags', array("count_articles" => 0));

		foreach($data as $k => $v){
			$where = Zend_Registry::get('db')->quoteInto('id = ?', $v['id_tag']);
			Zend_Registry::get('db')->update('tags', array("count_articles" => $v['count']), $where);
		}

    	die('done');
    }

    //reload images for objects
    public function imagesobjectsAction()
    {
    	set_time_limit(0);

    	$adapter = $this->getFileAdapter();

    	$data = Zend_Registry::get('db')->select()
					->from('objects', array('id', 'path', 'file_name', 'original_file_name'))
					->where('path != ?', '')
					->query()
					->fetchAll(Zend_Db::FETCH_ASSOC);

		foreach($data as $item)
		{
			if(trim($item['path']) == '') continue;
			$file['type'] = 'image/jpeg';
			$file['error'] = 0;
			$file['name'] = $item['id'].'original'.$item['file_name'];
			$this->clear_files($item['path'], $file['name']);
			$d = $this->LoadImage($file, $item['path'], $item['id'], 'objects');
			$where = Zend_Registry::get('db')->quoteInto('id = ?', $item['id']);
			Zend_Registry::get('db')->update('objects', $d, $where);
		}

		die('done');
    }

    //reload images for users
    public function imagesusersAction()
    {
    	set_time_limit(0);
    	$data = Zend_Registry::get('db')->select()
					->from('accounts', array('id', 'path', 'file_name'))
					->where('path != ?', '')
					->query()
					->fetchAll(Zend_Db::FETCH_ASSOC);

		foreach($data as $item)
		{
			if(trim($item['path']) == '') continue;
			$file['type'] = 'image/jpeg';
			$file['error'] = 0;
			$file['name'] = $item['file_name'];
			$this->clear_files($item['path'], $item['file_name']);
			$this->LoadImage($file, $item['path'], $item, 'users');
		}

		die('done');
    }

    //reload images for groups
    public function imagesgroupsAction()
    {
    	set_time_limit(0);
    	$data = Zend_Registry::get('db')->select()
					->from('groups', array('id', 'path', 'file_name'))
					->query()
					->fetchAll(Zend_Db::FETCH_ASSOC);

		foreach($data as $item)
		{
			if(trim($item['path']) == '') continue;
			$file['type'] = 'image/jpeg';
			$file['error'] = 0;
			$file['name'] = $item['file_name'];

			$this->clear_files($item['path'], $item['file_name']);

			$this->LoadImage($file, $item['path'], $item, 'groups');
		}

		die('done');
    }

    //reload images for articles
    public function citiesKoatuuAction()
    {
    	set_time_limit(0);
    	$data = Zend_Registry::get('db')->select()
					->from('districts')
					->query()
					->fetchAll(Zend_Db::FETCH_ASSOC);

		foreach($data as $item)
		{
			$d['id_district'] = $item['id'];
			$d['id_region'] = $item['id_region'];
			$where = Zend_Registry::get('db')->quoteInto('district_koatuu = ?', $item['koatuu']);
			Zend_Registry::get('db')->update('cities', $d, $where);
		}

		die('done');
    }

    //Clear users
    public function clearusersAction(){
    	set_time_limit(0);
    	Zend_Registry::get('db')->delete('accounts', 'id > 21');
    	die('Users has been cleared!');
	}


    //load users images from phpbb_users
    public function loadusersavatarsAction()
    {
    	set_time_limit(0);

    	$curr_id = 20;
    	$items_on_page = 500;

		$data = Zend_Registry::get('db')->select()
							->from('accounts',  array('count' => 'count(id)'))
							->where('id > ?', $curr_id)
							->order('id')
							->query()
							->fetchAll(Zend_Db::FETCH_ASSOC);

    	$count_items = $data[0]['count'];
    	$pages = intval($count_items/$items_on_page);

    	for($page=1; $page <= $pages; $page++)
    	{
			$data = Zend_Registry::get('db')->select()
						->from('accounts', array('id', 'path', 'file_name'))
						->where('id > ?', $curr_id)
						->order('id')
						->limitPage($page, $items_on_page)
						->query()
						->fetchAll(Zend_Db::FETCH_ASSOC);

			foreach($data as $item)
			{
				$d = array();

				if(trim($item['file_name']) != '')
				{
					//$ex = substr($item['file_name'], strlen($item['file_name'])-3, 3);
					//echo $ex.'<br>';
					//if(strtolower($ex) == 'jpg')
					{
						$d['path'] = '/users/'.intval($item['id']/1000).'/'.$item['id'].'/';
				    	$this->_files->CreateFolder(intval($item['id']/1000), '/users');
				    	$this->_files->CreateFolder($item['id'], '/users/'.intval($item['id']/1000));

				    	if(substr($item['file_name'], 0, 7) == 'http://')
					    	try{
					    		 copy($item['file_name'], $this->_files_path.$d['path'].$item['id'].'.jpg');
					    	}catch (Exception $e){}
				    	else
				    	try{
				    		copy('http://www.lancer.com.ua/forum/images/avatars/'.$item['file_name'],
				    			 $this->_files_path.$d['path'].$item['file_name']);
				    	}catch (Exception $e){}

						$where = Zend_Registry::get('db')->quoteInto('id = ?', $item['id']);
						Zend_Registry::get('db')->update('accounts', $d, $where);
					}
				}

			}
    	}

    	die('done');
    }

    //load articles from mos_content
    public function loadarticlesAction()
    {
    	set_time_limit(0);

    	$curr_id = 0;

    	$items_on_page = 500;

		$data = Zend_Registry::get('db')->select()
							->from('mos_content',  array('count' => 'count(id)'))
							->where('id > ?', $curr_id)
							->order('id')
							->query()
							->fetchAll(Zend_Db::FETCH_ASSOC);

    	$count_items = $data[0]['count'];
    	$pages = intval($count_items/$items_on_page);

    	//for($page=1; $page <= $pages; $page++)
    	{
		    	$data = Zend_Registry::get('db')->select()
							->from('mos_content')
							->where('id > ?', $curr_id)
							->order('id')
							//->limitPage($page, $items_on_page)
							->query()
							->fetchAll(Zend_Db::FETCH_ASSOC);

				foreach($data as $item)
				{
					$year = substr($item['created'], 0, 4);
					$mon = substr($item['created'], 5, 2);
					$day = substr($item['created'], 8, 2);
					$h = substr($item['created'], 11, 2);
					$m = substr($item['created'], 14, 2);
					$s = substr($item['created'], 17, 2);

					$created_date = mktime($h, $m, $s, $mon, $day, $year);

					$d['id_cat'] = 111;
					$d['add_user_id'] = 16;
					$d['modify_user_id'] = 16;
					$d['title'] = $item['title'];
					$d['add_date'] = $created_date;
					$d['modify_date'] = $created_date;
					$d['public_date'] = $created_date;

					$d['desc'] = $this->imagesTagsComplete($item['introtext']);//'<p>'.strip_tags($item['introtext']).'</p>';
					$d['body'] = $this->imagesTagsComplete($item['introtext']).$this->imagesTagsComplete($item['fulltext']);
					$d['is_active'] = 1;
					$d['is_removed'] = 0;
					$d['is_ghost'] = 0;
					$d['is_public'] = 1;
					$d['type'] = 0;

					$d['hit_count'] = $item['hits'];


					Zend_Registry::get('db')->insert('articles', $d);

				}
    	}

		die('done');
    }

    public function imagesTagsComplete($text)
    {
    	return str_replace('/images/stories/', 'http://www.lancer.com.ua/images/stories/', $text);
    }

    //Clear articles
    public function cleararticlesAction(){
    	set_time_limit(0);
    	Zend_Registry::get('db')->delete('articles', 'id > 28');
    	die('Articles has been cleared!');
	}

	private function clear_files($path, $donotremove = null)
	{
	  if($dir = @opendir($this->_path_files.$path))
	  {
	     while (($file = readdir($dir)) !== false)
	       if ($file != "." && $file != ".." && $file != $donotremove)
	       {
	       	  unlink($this->_path_files.$path."/".$file);
	       }
	  }
	}
}
